<?php
require './include/global.php';
require './include/validate.php';
require './include/all_left.php';
require_once './include/advisory_db.php';
$smarty->display('header.tpl');

$emailId = $_GET['emailId'] +0;
$smarty->assign("emailId",$emailId);
if($emailId < 1) {
   echo "<meta http-equiv=\"refresh\" content=\"0;URL=errorall.php?errid=rc_1012\">";
   exit;	
}

$sql = "SELECT * FROM pa_email WHERE e_id = '".$emailId."'";
$emailInfo = $DB->fetch_one_array($sql);

if(empty($emailInfo)){
   echo "<meta http-equiv=\"refresh\" content=\"0;URL=errorall.php?errid=rc_1012\">";
   exit;	
}

$fromId = $emailInfo['e_sentrcid'];
$toId	= $emailInfo['e_receivedrcid'];

if(!empty($_POST) && trim($_POST['submit']) == "Accept Invite"){

	if(areFriends( $fromId, $toId )){
		 echo "<meta http-equiv=\"refresh\" content=\"0;URL=errorall.php?errid=rc_1015\">";
	     exit;	
	}

	$fromRcInfo = $DB->fetch_one_array("SELECT * FROM pa_recuiter WHERE rc_id = '".$fromId."' ");
	$toRcInfo = $DB->fetch_one_array("SELECT * FROM pa_recuiter WHERE rc_id = '".$toId."' ");

	//sender to receiver
	$checkAtoB = $DB->fetch_one_array("SELECT count(*) as count FROM pa_mycontacts WHERE myc_rcid = '".$fromId."' AND myc_fid = '".$toId."' ");
	if($checkAtoB['count']<1){
		$sql = "
		INSERT INTO pa_mycontacts (myc_rcid,myc_fid, myc_firstname, myc_lastname, myc_email, myc_jobtitle, myc_city, myc_state, myc_zipcode, myc_country, myc_industry, myc_created) VALUES (
			'".$fromId."',
			'".$toId."',
			'".$toRcInfo[rc_fname]."',
			'".$toRcInfo[rc_lname]."',
			'".$toRcInfo[rc_email]."',
			'".$toRcInfo[rc_title]."',
			'".$toRcInfo[rc_city]."',
			'".$toRcInfo[rc_state]."',
			'".$toRcInfo[rc_zip]."',
			'".$toRcInfo[rc_country]."',
			'".$toRcInfo[rc_industry]."',
			'".time()."'
		)";
		$DB->query($sql);
	}

	//receiver to sender
	$checkBtoA = $DB->fetch_one_array("SELECT count(*) as count FROM pa_mycontacts WHERE myc_rcid = '".$toId."' AND myc_fid = '".$fromId."' ");
	if($checkBtoA['count']<1){
		$sql = "
		INSERT INTO pa_mycontacts (myc_rcid,myc_fid, myc_firstname, myc_lastname, myc_email, myc_jobtitle, myc_city, myc_state, myc_zipcode, myc_country, myc_industry, myc_created) VALUES (
			'".$toId."',
			'".$fromId."',
			'".$fromRcInfo[rc_fname]."',
			'".$fromRcInfo[rc_lname]."',
			'".$fromRcInfo[rc_email]."',
			'".$fromRcInfo[rc_title]."',
			'".$fromRcInfo[rc_city]."',
			'".$fromRcInfo[rc_state]."',
			'".$fromRcInfo[rc_zip]."',
			'".$fromRcInfo[rc_country]."',
			'".$fromRcInfo[rc_industry]."',
			'".time()."'
		)";
		$DB->query($sql);
	}


	//Save Email receiver to sender
	$sql = " INSERT INTO pa_email (
		`e_sentrcid`,
		`e_receivedrcid`,
		`e_fromemail`,
		`e_fromname`,
		`e_sendertype`,
		`e_receivertype`,
		`e_type`,
		`e_toemail`,
		`e_toname`,
		`e_subject`,
		`e_content`,
		`e_isreplied`,
		`e_created`				
	) VALUES (
		 '".$_SESSION['uid']."',
		 '".$fromId."',
		 '".$_SESSION['useremail']."',
		 '".$_SESSION['realname']."',
		 'recruiter',
		 'recruiter',
		 '3',
		 '".$fromRcInfo['rc_email']."',
		 '".$fromRcInfo['rc_fname']." ".$fromRcInfo['rc_lname']."',
		 'Invitation Accepted',
		 '".$_POST['content']."',
		 1,
		 '".time()."'
	)";
	$DB->query($sql);
	
	//update the old email;
	$DB->query("UPDATE pa_email SET `e_isreplied` = 1 WHERE e_id = '".$emailId."'");

	$smarty->assign("success",1);


	$content = stripslashes($_POST['content']). "<br><br>";
	$content .= '<span style="color:gray;">Connect on <font color="orange">enetRecruiter</font> to achieve greater recruiting success.  Register for a free account today at <a href="http://www.enetrecruiter.com/">enetRecruiter.com</a></span>'."<br><br>";
	$content .= '<span style="color:gray;">NOTE: This message was sent to you by a member of enetRecruiter.com. Please do not reply to this message. To reply to the sender, please sign-in to your account on enetRecruiter or create an account if you are not yet a member. For all questions, please contact us at <a href="mailto:account@enetrecruiter.com">account@enetrecruiter.com</a>. Wishing you many recruiting success!</span>'. "<br><br>";


	//Send Email
	require_once("./include/class.phpmailer.php");
	$mail=new PHPMailer();

	$mail->IsMail();
	$mail->From     = "noreply@enetrecruiter.com";
	$mail->FromName = $_SESSION['realname'];
	$mail->AddAddress($fromRcInfo['rc_email'],$fromRcInfo['rc_fname']." ".$fromRcInfo['rc_lname']);

	$mail->IsHTML(true);
	$mail->Subject = "Your invitation to connect was accepted";
	$mail->Body = stripslashes($content);

	$mail->CharSet = 'UTF-8';

	$mail->Send();


}
$smarty->assign("fromuid",$fromId);
$smarty->assign("fromname",$emailInfo['e_fromname']);

$smarty->display('allleft.tpl');
$smarty->display('replyinvite.tpl');
$smarty->display('allright.tpl');
$smarty->display('footer.tpl');
?>
